package com.sense.kukoo.common.wiget.dialog

import android.app.Activity
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.*
import android.widget.PopupWindow
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.feel.common.global.bean.WebPage
import com.sense.kukoo.common.R
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.constant.ConstantConfig
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.lib_logger.LoggerManager

class ContentAgreementPopupWindow(
    contentView:View, width:Int, height:Int, focusable:Boolean
): PopupWindow(contentView,  width,  height,  focusable) {

    private val TAG: String = javaClass.simpleName

    private lateinit var root:View
    private lateinit var content:View
    private lateinit var iv_bottom:View

    companion object{
        fun create(activity: Activity,  root: ViewGroup?):ContentAgreementPopupWindow{
            val inflate: View = LayoutInflater.from(activity).inflate(R.layout.common_popup_content_agreement, root)
            val width =  WindowManager.LayoutParams.MATCH_PARENT
            val height =  WindowManager.LayoutParams.MATCH_PARENT

            val popupWindow = ContentAgreementPopupWindow(inflate, width, height, true)
            popupWindow.root = inflate.findViewById(R.id.root_content_agreement)
            popupWindow.content = inflate.findViewById(R.id.content_content_agreement)
            popupWindow.iv_bottom = inflate.findViewById(R.id.iv_bottom)
            //
            popupWindow.root.setOnClickListener {
                popupWindow.dismiss()
            }
            //
            popupWindow.isTouchable = true
            popupWindow.isOutsideTouchable = true
            popupWindow.elevation = 4f
            //
            val tv_title = popupWindow.contentView.findViewById<TextView>(R.id.tv_title)
            //
            val text1 = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_content_agreement_text_1)
            val text2 = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_content_agreement_text_2)
            val text3 = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_content_agreement_text_3)
            val color = ApplicationContext.getColor(R.color.dialog_btn_text_color_red)
            val click = object : ClickableSpan() {
                override fun onClick(widget: View) {
                    popupWindow.dismiss()
                    //
                    ARouter.getInstance()
                        .build(ARouterUrl.ROUTE_LOGIN_MODULE_WEB_ACTIVITY)
                        .withParcelable(ConstantConfig.ARG1, WebPage(url = ConstantConfig.URL.h5UploadAgreement()))
                        .navigation()
                }

                override fun updateDrawState(ds: TextPaint) = ds.setColor(color)
            }
            //
            val spannable = SpannableStringBuilder()
            spannable.append(text1)
                .append(text2, click, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                .append(text3)
            //
            tv_title.text = spannable
            tv_title.movementMethod = LinkMovementMethod.getInstance()
            tv_title.highlightColor = Color.TRANSPARENT

            return popupWindow
        }
    }

    private var toInit = true

    private fun toInitSize(anchor: View){
        //
        val location1:IntArray = intArrayOf(0,0)
        val location2:IntArray = intArrayOf(0,0)
        val location3:IntArray = intArrayOf(0,0)
        anchor.getLocationInWindow(location1)
        content.getLocationInWindow(location2)
        iv_bottom.getLocationInWindow(location3)
        //
        if(location3[0] <= 0){
            toInit = true
        }else{
            //
            val offX = location1[0] - location3[0] + ((anchor.width -  iv_bottom.width) / 2)
            val offY = location1[1] - location2[1] - (content.height + anchor.height * 2.5)
            //
            /*LoggerManager.e(TAG, "location1[0]=${location1[0]} - location1[1]=${location1[1]}")
            LoggerManager.e(TAG, "location2[0]=${location2[0]} - location2[1]=${location2[1]}")
            LoggerManager.e(TAG, "location3[0]=${location3[0]} - location3[1]=${location3[1]}")
            LoggerManager.e(TAG, "offX=${offX}, offY=${offY}")*/
            //
            content.translationY = offY.toFloat()
            iv_bottom.translationX = offX.toFloat()
        }

    }

    fun show(anchor: View): ContentAgreementPopupWindow {
        showAsDropDown(anchor, 0, 0)
        //
        root.alpha = 0F
        root.animate().alpha(1F).setDuration(300).setUpdateListener {
            if (toInit) {
                toInit = false
                toInitSize(anchor)
            }
        }.start()

        return this
    }


}